/**
 * 根据配置生成SVG头像的Data URI
 * @param {object | null} config - 头像配置对象
 * @returns {string} - SVG图像的Data URI
 */
export function generateAvatarSvg(config) {
	// 提供一个默认配置，以防传入null或无效配置
	if (!config || typeof config !== 'object') {
		config = { hair: 'short', hairColor: '#333', expression: 'happy', clothesColor: '#6a5acd' };
	}

	const { hair, hairColor, expression, clothesColor } = config;

	// 定义SVG各部分的绘制路径和样式
	const hairStyles = {
		short: `<rect x="15" y="8" width="34" height="15" fill="${hairColor || '#333'}" />`,
		long: `
			<rect x="15" y="8" width="34" height="12" fill="${hairColor || '#333'}" />
			<rect x="12" y="20" width="40" height="8" fill="${hairColor || '#333'}" />
		`
	};

	const expressionStyles = {
		happy: `
			<circle cx="24" cy="24" r="2" fill="#000" />
			<circle cx="40" cy="24" r="2" fill="#000" />
			<path d="M 26 34 A 8 8 0 0 0 38 34" stroke="#000" stroke-width="2" fill="none" />
		`,
		sad: `
			<rect x="22" y="23" width="4" height="4" fill="#000" />
			<rect x="38" y="23" width="4" height="4" fill="#000" />
			<path d="M 26 36 A 8 8 0 0 1 38 36" stroke="#000" stroke-width="2" fill="none" />
		`
	};

	const hairSvg = hairStyles[hair] || hairStyles['short'];
	const expressionSvg = expressionStyles[expression] || expressionStyles['happy'];

	const svgContent = `
		<svg width="64" height="64" viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg">
			<rect width="64" height="64" fill="#f0f2f5" />
			<g>
				<!-- 身体/衣服 -->
				<rect x="12" y="44" width="40" height="20" fill="${clothesColor || '#6a5acd'}" />
				<!-- 头部 -->
				<circle cx="32" cy="28" r="16" fill="#ffdead" />
				<!-- 头发 -->
				${hairSvg}
				<!-- 表情 -->
				${expressionSvg}
			</g>
		</svg>
	`;

	// 对SVG内容进行编码以便在Data URI中使用
	const encodedSvg = encodeURIComponent(svgContent
		.replace(/\s{2,}/g, ' ') // 移除多余的空格
		.replace(/>\s+</g, '><') // 移除标签间的空格
	);

	return `data:image/svg+xml,${encodedSvg}`;
} 